/*
 * @lc app=leetcode.cn id=434 lang=javascript
 *
 * [434] 字符串中的单词数
 */

// @lc code=start
/**
 * @param {string} s
 * @return {number}
 */
var countSegments = function(s) {
    if(s.length==0)return 0
    return s.split(" ").filter((item)=>{
        return item!=""
    }).length
};
// @lc code=end

var countSegments = function(s) {
    let count = 0;
    s += ' ';
    for (let p = 1; p < s.length; p++){
        if (s[p - 1] !== s[p] && s[p] === ' ') count++;
    }
    return count;
};//不用库函数

